package com.xiaojie.hadoop.mapreduce.combine;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.CombineTextInputFormat;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

/**
 * @author 熟透的蜗牛
 * @version 1.0
 * @description: TODO
 * @date 2024/12/27 9:23
 */
public class WordCountDriver {

    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {

        // 1 获取配置信息以及获取job对象
        Configuration configuration = new Configuration();
        Job job = Job.getInstance(configuration);

        // 2 关联本Driver程序的jar
        job.setJarByClass(WordCountDriver.class);
        // 3 关联Mapper和Reducer的jar
        job.setMapperClass(WordCountMapper.class);
        job.setReducerClass(WordCountReducer.class);

        // 4 设置Mapper输出的kv类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);
        // 5 设置最终输出kv类型

        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
         //设置InputFormat ，默认为TextInputFormat
        job.setInputFormatClass(CombineTextInputFormat.class);
        //虚拟存储大小设置 ，修改虚拟存储大小会改变切片的数量
        CombineTextInputFormat.setMaxInputSplitSize(job, 20*1024*1024);
        // 6 设置输入和输出路径
        FileInputFormat.setInputPaths(job, new Path("D:\\hadoop\\combine"));
        FileOutputFormat.setOutputPath(job, new Path("D:\\hadoop\\combinetextformat"));

        // 7 提交job
        boolean result = job.waitForCompletion(true);
        System.exit(result ? 0 : 1);

    }
}
